﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebShopData;

namespace GalaxySale2014
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Order currentOrder = (Order)Session["CurrentOrder"];
                TextBoxFirstName.Text = currentOrder.FirstName;
                TextBoxLastName.Text = currentOrder.LastName;
                TextBoxAddress.Text = currentOrder.Address;
                TextBoxCity.Text = currentOrder.City;
                TextBoxZip.Text = currentOrder.Zip;
                TextBoxEmail.Text = currentOrder.Email;

                GridViewOverview.DataSource = currentOrder.OrderRows;
                GridViewOverview.DataBind();

                foreach (GridViewRow row in GridViewOverview.Rows)
                {
                    OrderRow orderRow = currentOrder.GetOrderRow(int.Parse(row.Cells[0].Text.Trim()));
                    TextBox textBoxQ = (TextBox)row.FindControl("TextBoxQ");
                    textBoxQ.Text = orderRow.Quantity.ToString();
                }
                ShowTotalSum();
            }
        }

        private void ShowTotalSum()
        {
            Order currentOrder = (Order)Session["CurrentOrder"];
            long totalSum = currentOrder.TotalSum;
            if (totalSum == 0)
            {
                LabelCartTotalSum.Text = "Cart is Empty.";
                ButtonSaveOrder.Visible = false;
            }
            else
            {
                LabelCartTotalSum.Text = string.Format("Total sum: {0:C0}", totalSum);
                ButtonSaveOrder.Visible = true;
            }
        }

        protected void ButtonSaveOrder_Click(object sender, EventArgs e)
        {
            Page.Validate();
            if (Page.IsValid)
            {
                Order currentOrder = (Order)Session["CurrentOrder"];
                FillCurrentOrderFromTextBox();
                //foreach (GridViewRow gridViewRow in GridViewOverview.Rows)
                //{
                //    OrderRow orderRow = currentOrder.GetOrderRow(int.Parse(gridViewRow.Cells[0].Text.Trim()));
                //    TextBox textBox = (TextBox)gridViewRow.FindControl("textBox");
                //    orderRow.Quantity = int.Parse(textBox.Text.Trim());
                //}
                Response.Redirect(@"~/ConfirmOrder.aspx");
                
            }
        }

        private void FillCurrentOrderFromTextBox()
        {
            Order currentOrder = (Order)Session["CurrentOrder"];
            currentOrder.FirstName = TextBoxFirstName.Text.Trim();
            currentOrder.LastName = TextBoxLastName.Text.Trim();
            currentOrder.Address = TextBoxAddress.Text.Trim();
            currentOrder.City = TextBoxCity.Text.Trim();
            currentOrder.Zip = TextBoxZip.Text.Trim();
            currentOrder.Email = TextBoxEmail.Text.Trim();
        }

        protected void GridViewOverview_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = GridViewOverview.Rows[index];
                Order currentOrder = (Order)Session["CurrentOrder"];
                currentOrder.DeleteOrderRow(int.Parse(row.Cells[0].Text.Trim()));

                Response.Redirect("~/Cart.aspx");
            }
        }

        protected void TextBoxQ_TextChanged(object sender, EventArgs e)
        {
            TextBox textBoxQ = (TextBox)sender;
            GridViewRow gridViewRow = (GridViewRow)textBoxQ.Parent.Parent;
            OrderRow orderRow = ((Order)Session["CurrentOrder"]).GetOrderRow(int.Parse(gridViewRow.Cells[0].Text.Trim()));
            orderRow.Quantity = (int.Parse(textBoxQ.Text.Trim()) > 0) ? int.Parse(textBoxQ.Text.Trim()) : orderRow.Quantity;
            ShowTotalSum();
        }
    }
}